* 2: Seat-level estimates
* Figures 1, 2, A1, A5
* Tables 3, 4, 5, A5, A6, A7, A12, A13, A14, A15
* Footnotes 20, 30; Numbers in Section 4.2
gl data ".../Replication Files/data"



* Create Dataset with Electoral Outcomes & GP Population
{

* 1 - Import and Append Raw Election Data
{
set more off
foreach cand in Sarpanch Member {
foreach date in Dec_2011 April_2012 July_2012 {
import excel "$data/GP_`cand'.xlsx", sheet("`date'") firstrow clear
if "`cand'"!="Sarpanch" | "`date'"!="Dec_2011" {
append using "$data/elecdata.dta"
}
save "$data/elecdata.dta", replace
}
}
}
*



* 2 - Clean, Create Variables
* Note: Some GP Names are in Gujarati, some are in English
{
use "$data/elecdata.dta", clear

drop if District==""
ren Word Ward

gen sarpanch = (Ward==.)
replace Ward=0 if sarpanch==1

duplicates tag District Taluka GPName Ward, gen(candidates)
replace candidates = candidates + 1
egen gp = group(District Taluka GPName)

gen winner = Winner=="Y" | Uncontestant=="Y"
gsort District Taluka GPName Ward -Votes -winner

gen nocontest = (candidates==1)

egen educ = group(Qual)
gen educ_years = 15 if educ!=.
replace educ_years = 17 if educ==1
replace educ_years = 0 if educ==2
replace educ_years = 10 if educ==3
replace educ_years = 12 if educ==4
replace educ_years = 7 if educ==5
drop Qual educ

gen female = Gender=="F" if Gender!=""

egen prof = group(Proff)
gen prof_ag_farmer = prof==2 if prof!=.
gen prof_ag_lab = prof==5 if prof!=.
gen prof_bus = prof==3 if prof!=.
gen prof_jobs = prof==4 if prof!=.
gen prof_other = prof==1 if prof!=.
drop prof

ren Name Name
replace Name = upper(Name)
split Name

gen lastname = Name6
forv n = 5(-1)1 {
replace lastname = Name`n' if lastname==""
}
replace lastname = subinstr(lastname," ","",.)
replace lastname = subinstr(lastname,")","",.)
replace lastname = subinstr(lastname,"(","",.)
replace lastname = subinstr(lastname,",","",.)
replace lastname = subinstr(lastname,".","",.)
replace lastname = subinstr(lastname,";","",.)
replace lastname = subinstr(lastname,":","",.)
replace lastname = subinstr(lastname,"/","",.)
drop if lastname==""
merge m:1 lastname using "$data/lastnames.dta", keepusing(*reweight)

egen seattype = group(SeatType)
la var seat "ST, STW, SC, SCW, OBC, OBCW, GENW, GEN" 

* Store characteristic_winner as empty when # "winners" is not 1
bysort District Taluka GPName Ward: egen num_winners = sum(winner)
ren Age age
foreach variable in age educ_years female prof_ag_farmer prof_ag_lab prof_bus prof_jobs prof_other prob_gen_reweight prob_obc_reweight prob_sc_reweight prob_st_reweight {
gen `variable'_winner = `variable' if winner==1
replace `variable'_winner = . if num_winners!=1
}

keep District Taluka GPName Ward Gender age Proffession SeatType Uncontestant Winner Votes sarpanch candidates gp winner nocontest educ_years female prof_ag_farmer prof_ag_lab prof_bus prof_jobs prof_other prob_gen* prob_obc* prob_sc* prob_st* seattype num_winners age_winner educ_years_winner female_winner prof_ag_farmer_winner prof_ag_lab_winner prof_bus_winner prof_jobs_winner prof_other_winner

bysort gp: egen maxwards = max(Ward)

gen wards = 1 if sarpanch!=1
ren Ward ward_id

drop if gp==.
save "$data/elecdata_clean.dta", replace
}
*



* 3 - [GP Names LGD.xlsx] links Gujarati GP Names with LGD Codes and English GP Names
* Use this dataset to link Election Data with LGD Codes
{
* i. LGD IDs
{
import excel "$data/GP Names LGD.xlsx", sheet("LGD_Copy") firstrow clear
keep SNo DistrictName BlockName PanchayatName
ren SNo lgd_id
save "$data/lgd_temp", replace
}
*



* ii. Clean [GP Names LGD.xlsx]
{
import excel "$data/GP Names LGD.xlsx", sheet("Sheet1") cellrange(B4:I13364) firstrow clear

ren B District 
ren C Taluka
replace District = upper(subinstr(District," ","",.))
replace Taluka = upper(subinstr(Taluka," ","",.))
drop if District=="TOTAL" | Taluka=="TOTAL"
replace Taluka=Taluka[_n-1] if Taluka==""
ren D GPName
drop if District=="" | Taluka=="" | GPName==""

ren SrNo lgd_id
la var lgd_id "LGD Code"

duplicates drop District Taluka GPName, force
drop if lgd_id==.

duplicates drop District* Taluka BlockName GPName PanchayatName, force
save "$data/GP_EnglishNames.dta", replace
}
*



* iii. Fuzzy Merge GP Names from Election Data (English or Gujarati) with [GP Names LGD.xlsx]
{
use "$data/elecdata_clean.dta", clear
drop if District==District[_n-1] & Taluka==Taluka[_n-1] &  GPName==GPName[_n-1]
keep District Taluka GPName gp
save "$data/elecdata_clean_m.dta", replace



* Fuzzy Merge: District - Block - Gujarati GP Names
use "$data/GP_EnglishNames.dta", clear
reclink District Taluka GPName using "$data/elecdata_clean_m.dta", gen(merge) idm(lgd_id) idu(gp)
save "$data/temp_1.dta", replace



* Fuzzy Merge: District - Block - English GP Names
use "$data/GP_EnglishNames.dta", clear

ren GPName GPName_Gujarati
ren PanchayatName GPName
drop if GPName==""
 
replace GPName = subinstr(GPName,")","",.)
replace GPName = subinstr(GPName,"(","",.)

reclink District Taluka GPName using "$data/elecdata_clean_m.dta", gen(merge) idm(lgd_id) idu(gp)
save "$data/temp_2.dta", replace



* Remove duplicate matches
use "$data/temp_2.dta", clear
append using "$data/temp_1.dta"
drop if _merge!=3
drop _merge

bysort lgd_id: egen max_score = max(merge)
drop if merge!=max_score
drop max_score

bysort gp: egen max_score = max(merge)
drop if merge!=max_score
drop max_score

* Manual clean for duplicates that survive, by looking through GPName UGPName GPName_Gujarati
* For those w/exact, multiple matches on both English and Gujarati names, keep the English Names
{
duplicates tag lgd_id, gen(tag)
br lgd_id merge gp if tag>0

drop if lgd_id==75 & gp==50
drop if lgd_id==88 & gp==97
drop if lgd_id==1271
drop if lgd_id==546 & gp==542
drop if lgd_id==857 & gp==583
drop if lgd_id==3524 & gp==7331

drop if lgd_id==3560 & gp==7341
drop if lgd_id==3656 & gp==7164
drop if lgd_id==3700 & gp==7162
drop if lgd_id==3794
drop if lgd_id==3825 & gp==7245

drop if lgd_id==4318 & gp==7972
drop if lgd_id==4319 & gp==7971
drop if lgd_id==4516 & gp==8141
drop if lgd_id==4560 & gp==8149
drop if lgd_id==5334 & gp==9633

drop if lgd_id==6836
drop if lgd_id==7299
drop if lgd_id==8644 & gp==3641
drop if lgd_id==8894 & gp==1269
drop if lgd_id==9243 & gp==1652

drop if lgd_id==9279 & gp==1652
drop if lgd_id==9431 & gp==1416
drop if lgd_id==10530 & gp==8185
drop if lgd_id==10660 & gp==8612
drop if lgd_id==10665 & gp==8600

drop if lgd_id==11778 & gp==4525
drop if lgd_id==11888 & gp==4540
drop if lgd_id==12213 & gp==4699
drop if lgd_id==12465
drop if lgd_id==13033 & gp==5672

drop if lgd_id==13253 & gp==5534
drop if lgd_id==15667 & gp==11536
drop if lgd_id==16378
drop if lgd_id==16920 & gp==3904
drop if lgd_id==16921 & gp==3903

drop if lgd_id==16954 & gp==3958
drop tag

* check: duplicates report lgd_id

duplicates tag gp, gen(tag)
order gp merge lgd_id
sort gp
br lgd_id merge gp if tag>0

drop if gp==24 & lgd_id==138
drop if gp==1258 & lgd_id==8902
drop if gp==2202 & lgd_id==9931 
drop if gp==3674 & lgd_id==8662
drop if gp==4148 & lgd_id==6842

drop if gp==5147
drop if gp==5232 & lgd_id==12901
drop if gp==5371 & lgd_id==13186
drop if gp==5515 & lgd_id==13229
drop if gp==6330 & lgd_id==2797

drop if gp==6909 & lgd_id==3204
drop if gp==7121 & lgd_id==3384
drop if gp==7881 & lgd_id==4817
drop if gp==9425 & lgd_id==11678
drop if gp==9432 & lgd_id==11687

drop if gp==9662 & lgd_id==5282
drop if gp==9780 & lgd_id==5629
drop if gp==10127 & lgd_id==17989
drop if gp==10169 & lgd_id==17856
drop if gp==10722 & lgd_id==1582

drop if gp==10750 & lgd_id==1648
drop if gp==10758 & lgd_id==1659
drop if gp==10769 & lgd_id==1686
drop if gp==10819
drop if gp==11001 & lgd_id==15935
}
drop tag 

duplicates tag lgd_id, gen(tag)
replace gp=. if tag>0
replace merge=0 if tag>0
duplicates drop lgd_id gp, force

distinct lgd_id gp // no-duplicate check

save "$data/temp_3.dta", replace
}
*


}
*


}
*



* Footnote 20: Missingness Check
{
use "$data/vill_lgd_census11_01.dta", clear
merge 1:1 lgd_id using "$data/temp_3.dta", gen(merge0) 
keep if merge0!=2
gen missing = merge0==1

foreach c in 4001 5001 6001 8001 {
qui rdrobust missing gp_pop, c(`c') vce(cluster gp_pop) bwselect(msetwo) all
di "cutoff  " `c' "  b " round(e(tau_bc),.01) "  p-val: " round(e(pv_rb),.01) 
}
}
*



* Figure 1, A.1
* Numbers in Section 4.2
* Table A.5: Last Two Rows
{
use "$data/vill_lgd_census11_01.dta", clear
merge 1:1 lgd_id using "$data/temp_3.dta", gen(merge0) 

drop if gp==.
merge 1:m gp using "$data/elecdata_clean.dta", gen(merge00)

gen femwards0 = 1 if wards==1 & (seatt==2 | seatt==4 | seatt==6 | seatt==7)
gen stwards0 = 1 if wards==1 & (seatt==1 | seatt==2)
gen scwards0 = 1 if wards==1 & (seatt==3 | seatt==4)
gen obcwards0 = 1 if wards==1 & (seatt==5 | seatt==6)
keep if winner==1



* Numbers in Section 4.2, page 27
{
gen onecandidate = candidates==1
sum onecandidate
drop onecandidate

gen female_res = seatt==2 | seatt==4 | seatt==6 | seatt==7
tab female_res

sum female_winner if (seatt==1 | seatt==3 | seatt==5 | seatt==8)
}
*



foreach seattype in sc st obc fem {
bysort gp: egen `seattype'wards = sum(`seattype'wards0)
}
drop *wards0
duplicates drop gp, force 


gen regwards = maxwards - femwards


* Table A.2
foreach out in femwards regwards {
di "`out'"
foreach b in 5001 4001 6001 8001 {
qui rdrobust `out' gp_pop if inrange(gp_pop,`b'-1000,`b'+999), c(`b') all bwselect(msetwo)
di "cutoff  " `b' "  b " round(e(tau_bc),.001) "  se " round(e(se_tau_rb),.001) "  p-val: " round(e(pv_rb),.0001) 
}
di ""
}
*



la var maxwards "Number of Council Seats"
la var femwards "Council Seats Reserved for Women"

* Figure 1
{
scatter maxwards gp_pop if inrange(gp_pop,2000,10000) & maxwards<=21 & maxwards>5, msize(vtiny) jitter(1.75) xlabel(3001(1000)9001, labsize(vlarge)) xline(5001 8001) xtitle(Population, size(16pt)) xsc(titlegap(2)) graphregion(color(white)) ytitle(Seats, size(16pt)) ylabel(5(5)20, labsize(vlarge))


scatter femwards gp_pop if inrange(gp_pop,2000,10000) & maxwards<=21 & maxwards>5, msize(vtiny) jitter(1.75) xlabel(3001(1000)9001, labsize(vlarge) ) xline(5001 8001) xtitle(Population, size(16pt)) xsc(titlegap(2)) graphregion(color(white)) ytitle(Seats, size(16pt)) ylabel(0(2)8, labsize(vlarge))
}
*



* Figure A.1
{
foreach x in obcwards {
cmogram `x' gp_pop if gp_pop>2001 & gp_pop<10000,  histopts(bin(32)) lfitopts(lcolor(gs0) lwidth(medthick)) scatter graphopts(xtitle(Population) ytitle(Seats, size(16pt)) title("") graphregion(color(white)) mcolor(gs6) xtitle(Population, size(16pt)) xscale(range(2001 10000)) xlabel(4001 5001 6001 8001, labsize(vlarge)) xsc(titlegap(2))  xline(4001 5001 6001 8001) msize(small) ylabel(0(2)6, labsize(vlarge)) )
}


foreach x in scwards stwards {
cmogram `x' gp_pop if gp_pop>2001 & gp_pop<10000,  histopts(bin(32)) lfitopts(lcolor(gs0) lwidth(medthick)) scatter graphopts(xtitle(Population) ytitle(Seats, size(20pt)) title("") graphregion(color(white)) mcolor(gs6) xtitle(Population, size(20pt)) xscale(range(2001 10000)) xlabel(4001 5001 6001 8001, labsize(vlarge)) xsc(titlegap(2))  xline(4001 5001 6001 8001) msize(small) ylabel(0(2)6, labsize(vlarge)) )
}


}
*



}
*



* Table 3, 4, 5, A.6, A.7
* Figure 2, A5
* Footnote 30
{
use "$data/vill_lgd_census11_01.dta", clear
merge 1:1 lgd_id using "$data/temp_3.dta", gen(merge0) 

drop if gp==.
merge 1:m gp using "$data/elecdata_clean.dta", gen(merge00)

* Footnote 30
sum prob_obc_reweight_winner prob_sc_reweight_winner prob_st_reweight_winner if winner==1 & (seattype==7 | seattype==8)

gen prof_farmbus = prof_ag_farmer + prof_bus



* Table 3, Column 1
{

* Panel 1
{
local b = 5001 
di "cutoff  " `b' 

qui rdrobust cand gp_pop if winner==1 & inrange(gp_pop,`b'-1000,`b'+999), c(`b') all vce(cluster gp_pop) bwselect(msetwo)
di "b" round(e(tau_bc),.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb),.01) " "  round(e(ci_r_rb),.01) "]   "

qui sum cand if winner==1 & inrange(gp_pop,`b'-1000,`b'+999)
di "mean: " round(r(mean),.001)

di "obs:" r(N)
di ""
}
*



* Panel 2: Cutoffs = 4001, 6001
* 4001 estimate scaled by 9/11; 6001 estimate scaled by 13/11
{
gen c = 4001 
replace c = 6001 if gp_pop>=5001

gen bwselect = "msetwo" in 1
replace bwselect = "msetwo" in 2

gen vce = "cluster gp_pop" in 1
replace vce = "cluster gp_pop" in 2

qui rdmc cand gp_pop if winner==1 & inrange(gp_pop,3001,7000), c(c) bwselect(bwselect) vce(vce)
lincom (c1*9/11 + c2*13/11)/2

qui sum cand if winner==1 & inrange(gp_pop,3001,7000)
di "mean: " round(r(mean),.001)
di "obs:" r(N)
}
*



* Panel 3: Cutoff = 8001
* Estimate scaled by 17/11
{
local b = 8001 
di "cutoff  " `b' 

qui rdrobust cand gp_pop if winner==1 & inrange(gp_pop,`b'-1000,`b'+999), c(`b') all vce(cluster gp_pop) bwselect(msetwo)
di "b" round(e(tau_bc)*17/11,.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb)*17/11,.01) " "  round(e(ci_r_rb)*17/11,.01) "]   "

qui sum cand if winner==1 & inrange(gp_pop,`b'-1000,`b'+999)
di "mean: " round(r(mean),.001)

di "obs:" r(N)
di ""
}
*

}
*



* Figure 2
foreach b in 5001 {
qui rdrobust cand gp_pop if winner==1 & inrange(gp_pop,`b'-1000,`b'+999), c(`b') all vce(cluster gp_pop) bwselect(msetwo)
di "cutoff  " `b' "  b" round(e(tau_bc),.01) "  p-val: " round(e(pv_rb),.001) 

local min = max(e(h_r), e(h_l))
local bandround1 = round(`b'-`min',100)
local bandround2 = round(`b'+`min',100)

rdplot cand gp_pop if winner==1 & inrange(gp_pop,`b'-`min',`b'+`min'), c(`b') nbins(3) p(2) ci(95) graph_options(ylabel(2.2(.2)3.2, labsize(large)) graphregion(color(white)) ytitle(Number of Candidates, size(13.5pt)) xsc(titlegap(2))  xtitle(GP Population, size(13.5pt)) title("") legend(off) xlabel(`bandround1' 5001 `bandround2', labsize(large)))
}
*



* Table 4
gen female_seat = (seatt==2 | seatt==4 | seatt==6 | seatt==7)
forv s = 7(-2)1 {
di "seattype = `s'"
local b = 5001 

foreach out in cand educ_years age prof_farmbus {
qui rdrobust `out' gp_pop if winner==1 & (seattype==`s' | seattype==`s'+1) & inrange(gp_pop,`b'-1000,`b'+999), c(`b') all vce(cluster gp_pop) bwselect(msetwo)
di "b" round(e(tau_bc),.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb),.01) " "  round(e(ci_r_rb),.01) "]   "

qui sum `out' if winner==1 & (seattype==`s' | seattype==`s'+1) & inrange(gp_pop,`b'-1000,`b'+999)
di "mean: " round(r(mean),.001)

di "obs:" r(N)
di ""
}
*


foreach out in female_winner {
qui rdrobust `out' gp_pop if winner==1 & (seattype==`s' | seattype==`s'+1) & inrange(gp_pop,`b'-1000,`b'+999), c(`b') all vce(cluster gp_pop) bwselect(msetwo) covs(female_seat)
di "b" round(e(tau_bc),.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb),.01) " "  round(e(ci_r_rb),.01) "]   "

qui sum `out' if winner==1 & (seattype==`s' | seattype==`s'+1) & inrange(gp_pop,`b'-1000,`b'+999)
di "mean: " round(r(mean),.001)

di "obs:" r(N)
di ""
}
*
di " "

}
*




* Table A.6
* 4001 estimate scaled by 9/11; 6001 estimate scaled by 13/11
{
gen c = 4001 
replace c = 6001 if gp_pop>=5001

gen bwselect = "msetwo" in 1
replace bwselect = "msetwo" in 2

gen vce = "cluster gp_pop" in 1
replace vce = "cluster gp_pop" in 2

gen covs = "female_seat" in 1
replace covs = "female_seat" in 2

forv s = 7(-2)1 {
di "seattype = `s'"
foreach out in cand educ_years age prof_farmbus {
di "`out'"
qui rdmc `out' gp_pop if winner==1 & (seattype==`s' | seattype==`s'+1) & inrange(gp_pop,3001,7000), c(c) bwselect(bwselect) vce(vce)
lincom (c1*9/11 + c2*13/11)/2

qui sum `out' if winner==1 & (seattype==`s' | seattype==`s'+1) & inrange(gp_pop,3001,7000)
di "mean: " round(r(mean),.001)
di "obs:" r(N)
}
*

foreach out in female_winner {
di "`out'"
qui rdmc `out' gp_pop if winner==1 & (seattype==`s' | seattype==`s'+1) & inrange(gp_pop,3001,7000), c(c) bwselect(bwselect) vce(vce) covs(covs)
lincom (c1*9/11 + c2*13/11)/2

qui sum `out' if winner==1 & (seattype==`s' | seattype==`s'+1) & inrange(gp_pop,3001,7000)
di "mean: " round(r(mean),.001)
di "obs:" r(N)
}
*
}
*

}
*



* Table A.7
* Estimate scaled by 17/11
{
forv s = 7(-2)1 {
di "seattype = `s'"
local b = 8001 

foreach out in cand educ_years age prof_farmbus {
qui rdrobust `out' gp_pop if winner==1 & (seattype==`s' | seattype==`s'+1) & inrange(gp_pop,`b'-1000,`b'+999), c(`b') all vce(cluster gp_pop) bwselect(msetwo)
di "b" round(e(tau_bc)*17/11,.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb)*17/11,.01) ", "  round(e(ci_r_rb)*17/11,.01) "]   "

qui sum `out' if winner==1 & (seattype==`s' | seattype==`s'+1) & inrange(gp_pop,`b'-1000,`b'+999)
di "mean: " round(r(mean),.001)

di "obs:" r(N)
di ""
}
*

foreach out in female_winner {
qui rdrobust `out' gp_pop if winner==1 & (seattype==`s' | seattype==`s'+1) & inrange(gp_pop,`b'-1000,`b'+999), c(`b') all vce(cluster gp_pop) bwselect(msetwo) covs(female_seat)
di "b" round(e(tau_bc)*17/11,.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb)*17/11,.01) ", "  round(e(ci_r_rb)*17/11,.01) "]   "

qui sum `out' if winner==1 & (seattype==`s' | seattype==`s'+1) & inrange(gp_pop,`b'-1000,`b'+999)
di "mean: " round(r(mean),.001)

di "obs:" r(N)
di ""
}
*

di " "
}
}
*



* Figure A.5
foreach b in 5001 {
qui rdrobust educ_years gp_pop if winner==1 & inrange(gp_pop,`b'-1000,`b'+999), c(`b') all vce(cluster gp_pop) bwselect(msetwo)
di "cutoff  " `b' "  b" round(e(tau_bc),.01) "  p-val: " round(e(pv_rb),.001) 

local min = max(e(h_r), e(h_l))
local bandround1 = round(`b'-`min',100)
local bandround2 = round(`b'+`min',100)

rdplot educ_years gp_pop if winner==1 & seatt>=7 & gp_pop>=`b'-`min' & gp_pop<`b'+`min', c(`b') nbins(3) p(2) ci(95) graph_options(ylabel(4(1)9, labsize(large)) graphregion(color(white)) ytitle(Years of Education, size(13.5pt)) xsc(titlegap(2)) xtitle(GP Population, size(13.5pt)) title("") legend(off) xlabel(`bandround1' 5001 `bandround2', labsize(large)))

}
*



* Table 5
{
* Panel 1
foreach out in prob_obc_reweight prob_sc_reweight prob_st_reweight {
di "`out'"
local b = 5001

qui rdrobust `out' gp_pop if winner==1 & (seattype==7 | seattype==8) & inrange(gp_pop,`b'-1000,`b'+999), c(`b') all vce(cluster gp_pop) bwselect(msetwo)
di "b" round(e(tau_bc),.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb),.01) " "  round(e(ci_r_rb),.01) "]   "

qui sum `out' if winner==1 & (seattype==7 | seattype==8) & inrange(gp_pop,`b'-1000,`b'+999)
di "mean: " round(r(mean),.001)

di "obs:" r(N)
di ""
}
*



* Panel 2: Cutoffs = 4001, 6001
* 4001 estimate scaled by 9/11; 6001 estimate scaled by 13/11
foreach out in prob_obc_reweight prob_sc_reweight prob_st_reweight {
di "`out'"

qui rdmc `out' gp_pop if winner==1 & (seattype==7 | seattype==8) & inrange(gp_pop,3001,7000), c(c) bwselect(bwselect) vce(vce)
lincom (c1*9/11 + c2*13/11)/2

qui sum `out' if winner==1 & (seattype==7 | seattype==8) & inrange(gp_pop,3001,7000)
di "mean: " round(r(mean),.001)
di "obs:" r(N)
}
*




* Panel 3: Cutoff = 8001
* Estimate scaled by 17/11
foreach out in prob_obc_reweight prob_sc_reweight prob_st_reweight {
di "`out'"
local b = 8001 
qui rdrobust `out' gp_pop if winner==1 & (seattype==7 | seattype==8) & inrange(gp_pop,`b'-1000,`b'+999), c(`b') all vce(cluster gp_pop) bwselect(msetwo)
di "b" round(e(tau_bc)*17/11,.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb)*17/11,.01) " "  round(e(ci_r_rb)*17/11,.01) "]   "

qui sum `out' if winner==1 & (seattype==7 | seattype==8) & inrange(gp_pop,`b'-1000,`b'+999)
di "mean: " round(r(mean),.001)

di "obs:" r(N)
di ""
}
*


}
*


}
*



* Table A12, Panels 3-7
{
* Panel 3
foreach out in cand {
use "$data/vill_lgd_census11_01.dta", clear
merge 1:1 lgd_id using "$data/temp_3.dta", gen(merge0) 

drop if gp==.
merge 1:m gp using "$data/elecdata_clean.dta", gen(merge00)

local b = 5001


* alternative kernels
foreach kernel in uni epa {

qui rdrobust `out' gp_pop if winner==1 & inrange(gp_pop,`b'-1000,`b'+999), c(`b') all  vce(cluster gp_pop) bwselect(msetwo) kernel(`kernel')
di "b " round(e(tau_bc),.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb),.01) " "  round(e(ci_r_rb),.01) "]   "

}
*

* alternative local polynomial 
forv p = 2/3 {

qui rdrobust `out' gp_pop if winner==1 & inrange(gp_pop,`b'-1000,`b'+999), c(`b') all  vce(cluster gp_pop) bwselect(msetwo) p(`p') 
di "b " round(e(tau_bc),.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb),.01) " "  round(e(ci_r_rb),.01) "]   "

}
*


* no controls
{

qui rdrobust `out' gp_pop if winner==1 & inrange(gp_pop,`b'-1000,`b'+999), c(`b') all bwselect(msetwo) vce(cluster gp_pop) 
di "b " round(e(tau_bc),.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb),.01) " "  round(e(ci_r_rb),.01) "]   "

}
*


* including outliers
{

qui rdrobust `out' gp_pop if winner==1 & inrange(gp_pop,`b'-1000,`b'+999), c(`b') all bwselect(msetwo) vce(cluster gp_pop)
di "b " round(e(tau_bc),.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb),.01) " "  round(e(ci_r_rb),.01) "]   "

}
*

}
*


* Panels 4, 5, 7
{
use "$data/vill_lgd_census11_01.dta", clear
merge 1:1 lgd_id using "$data/temp_3.dta", gen(merge0) 

drop if gp==.
merge 1:m gp using "$data/elecdata_clean.dta", gen(merge00)

local b = 5001


foreach out in educ_years_winner age_winner prob_obc_reweight {
di "`out'"


* alternative kernels
foreach kernel in uni epa {

qui rdrobust `out' gp_pop if winner==1 & (seattype==7 | seattype==8) & inrange(gp_pop,`b'-1000,`b'+999), c(`b') all  vce(cluster gp_pop) bwselect(msetwo) kernel(`kernel')
di "b " round(e(tau_bc),.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb),.01) " "  round(e(ci_r_rb),.01) "]   "

}
*


* alternative local polynomial 
forv p = 2/3 {

qui rdrobust `out' gp_pop if winner==1 & (seattype==7 | seattype==8) & inrange(gp_pop,`b'-1000,`b'+999), c(`b') all  vce(cluster gp_pop) bwselect(msetwo) p(`p') 
di "b " round(e(tau_bc),.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb),.01) " "  round(e(ci_r_rb),.01) "]   "

}
*


* no controls (no change)
{

qui rdrobust `out' gp_pop if winner==1 & (seattype==7 | seattype==8) & inrange(gp_pop,`b'-1000,`b'+999), c(`b') all bwselect(msetwo) vce(cluster gp_pop) 
di "b " round(e(tau_bc),.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb),.01) " "  round(e(ci_r_rb),.01) "]   "

}
*


* including outliers (no change)
{

qui rdrobust `out' gp_pop if winner==1 & (seattype==7 | seattype==8) & inrange(gp_pop,`b'-1000,`b'+999), c(`b') all bwselect(msetwo) vce(cluster gp_pop)
di "b " round(e(tau_bc),.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb),.01) " "  round(e(ci_r_rb),.01) "]   "

}
*

}
*

}
*


* Panel 6
foreach out in female_winner {
use "$data/vill_lgd_census11_01.dta", clear
merge 1:1 lgd_id using "$data/temp_3.dta", gen(merge0) 

drop if gp==.
merge 1:m gp using "$data/elecdata_clean.dta", gen(merge00)
gen female_seat = (seatt==2 | seatt==4 | seatt==6 | seatt==7)

local b = 5001


* alternative kernels
foreach kernel in uni epa {
local one = `one' + 1

qui rdrobust `out' gp_pop if winner==1 & (seattype==7 | seattype==8) & inrange(gp_pop,`b'-1000,`b'+999), c(`b') all covs(female_seat) vce(cluster gp_pop) bwselect(msetwo) kernel(`kernel')
di "b " round(e(tau_bc),.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb),.01) " "  round(e(ci_r_rb),.01) "]   "

}
*


* alternative local polynomial 
forv p = 2/3 {

qui rdrobust `out' gp_pop if winner==1 & (seattype==7 | seattype==8) & inrange(gp_pop,`b'-1000,`b'+999), c(`b') all covs(female_seat) vce(cluster gp_pop) bwselect(msetwo) p(`p') 
di "b " round(e(tau_bc),.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb),.01) " "  round(e(ci_r_rb),.01) "]   "

}
*


* no controls
{

qui rdrobust `out' gp_pop if winner==1 & (seattype==7 | seattype==8) & inrange(gp_pop,`b'-1000,`b'+999), c(`b') all covs(female_seat) bwselect(msetwo) vce(cluster gp_pop) 
di "b " round(e(tau_bc),.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb),.01) " "  round(e(ci_r_rb),.01) "]   "

}
*


* including outliers (no change)
{

qui rdrobust `out' gp_pop if winner==1 & (seattype==7 | seattype==8) & inrange(gp_pop,`b'-1000,`b'+999), c(`b') all covs(female_seat) bwselect(msetwo) vce(cluster gp_pop)
di "b " round(e(tau_bc),.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb),.01) " "  round(e(ci_r_rb),.01) "]   "

}
*

}
*


}
*



* Table A13, Panels 3-7
{
use "$data/vill_lgd_census11_01.dta", clear
merge 1:1 lgd_id using "$data/temp_3.dta", gen(merge0) 

drop if gp==.
merge 1:m gp using "$data/elecdata_clean.dta", gen(merge00)
gen female_seat = (seatt==2 | seatt==4 | seatt==6 | seatt==7)



* Panel 3
foreach out in cand {
local b = 5001

foreach bw in mserd msetwo msesum msecomb2 cerrd certwo cersum cercomb2 {
di "cutoff `b'   `out'  `bw'"

qui rdrobust `out' gp_pop if winner==1 & inrange(gp_pop,`b'-1000,`b'+999), c(`b') all  vce(cluster gp_pop) bwselect(`bw')
di "b " round(e(tau_bc),.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb),.01) " "  round(e(ci_r_rb),.01) "]   "

di ""
}
}
*



* Panels 4, 5, 7
foreach out in  educ_years_winner age_winner  prob_obc_reweight {
local b = 5001

foreach bw in mserd msetwo msesum msecomb2 cerrd certwo cersum cercomb2 {
di "cutoff `b'   `out'  `bw'"

qui rdrobust `out' gp_pop if winner==1 & (seattype==7 | seattype==8) & inrange(gp_pop,`b'-1000,`b'+999), c(`b') all  vce(cluster gp_pop) bwselect(`bw')
di "b " round(e(tau_bc),.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb),.01) " "  round(e(ci_r_rb),.01) "]   "

di ""
}
*
}
*



* Panel 6
foreach out in female_winner {
local b = 5001

foreach bw in mserd msetwo msesum msecomb2 cerrd certwo cersum cercomb2 {
di "cutoff `b'   `out'  `bw'"

qui rdrobust `out' gp_pop if winner==1 & (seattype==7 | seattype==8) & inrange(gp_pop,`b'-1000,`b'+999), c(`b') all covs(female_seat) vce(cluster gp_pop) bwselect(`bw')
di "b " round(e(tau_bc),.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb),.01) " "  round(e(ci_r_rb),.01) "]   "

di ""
}

}
*


}
*



* Table A14
{

* Column 1
{
use "$data/Samras_GPs.dta", clear
keep lgd_id
keep if lgd_id!=.

merge 1:1 lgd_id using "$data/vill_lgd_census11_01.dta", gen(merge) 
gen samr = merge==3
bysort gp_id: egen samras=max(samr)
drop samr

merge 1:1 lgd_id using "$data/temp_3.dta", gen(merge0) 

drop if gp==.
merge 1:m gp using "$data/elecdata_clean.dta", gen(merge00)


gen female_seat = (seatt==2 | seatt==4 | seatt==6 | seatt==7)


local b = 5001
foreach out in educ_years_winner age_winner {
di "`out' all seats"
qui rdrobust `out' gp_pop if winner==1 & inrange(gp_pop,`b'-1000,`b'+999), c(`b') all fuzzy(samr) vce(cluster gp_pop) bwselect(msetwo)
di "b " round(e(tau_bc),.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb),.01) " "  round(e(ci_r_rb),.01) "]   "

di "`out' gen seats"
qui rdrobust `out' gp_pop if winner==1 & inrange(gp_pop,`b'-1000,`b'+999) & inrange(seatt,7,8), c(`b') all fuzzy(samr) vce(cluster gp_pop) bwselect(msetwo)
di "b " round(e(tau_bc),.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb),.01) " "  round(e(ci_r_rb),.01) "]   "
}
*



foreach out in female_winner {
di "`out' all seats"
qui rdrobust `out' gp_pop if winner==1 & inrange(gp_pop,`b'-1000,`b'+999), c(`b') all fuzzy(samr) vce(cluster gp_pop) bwselect(msetwo) covs(female_seat)
di "b " round(e(tau_bc),.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb),.01) " "  round(e(ci_r_rb),.01) "]   "

di "`out' gen seats"
qui rdrobust `out' gp_pop if winner==1 & inrange(gp_pop,`b'-1000,`b'+999) & inrange(seatt,7,8), c(`b') all fuzzy(samr) vce(cluster gp_pop) bwselect(msetwo) covs(female_seat)
di "b " round(e(tau_bc),.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb),.01) " "  round(e(ci_r_rb),.01) "]   "
}
*


di "prob_obc_reweight gen seats"
qui rdrobust prob_obc_reweight gp_pop if winner==1 & inrange(gp_pop,`b'-1000,`b'+999) & inrange(seatt,7,8), c(`b') all fuzzy(samr) vce(cluster gp_pop) bwselect(msetwo)
di "b " round(e(tau_bc),.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb),.01) " "  round(e(ci_r_rb),.01) "]   "

}
*



* Column 2
{
local b = 5001 
foreach out in educ_years age {

qui rdrobust `out' gp_pop if winner==1 & inrange(gp_pop,`b'-1000,`b'+999), c(`b') all fuzzy(cand) vce(cluster gp_pop) bwselect(msetwo)
di "b" round(e(tau_bc),.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb),.01) " "  round(e(ci_r_rb),.01) "]   "

}
*



foreach out in female {

qui rdrobust `out' gp_pop if winner==1 & inrange(gp_pop,`b'-1000,`b'+999), c(`b') all fuzzy(cand) vce(cluster gp_pop) bwselect(msetwo) covs(female_seat)
di "b" round(e(tau_bc),.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb),.01) " "  round(e(ci_r_rb),.01) "]   "

}
*



foreach out in female {

qui rdrobust `out' gp_pop if winner==1 & inrange(gp_pop,`b'-1000,`b'+999) & (seatt==7 | seatt==8), c(`b') all fuzzy(cand) vce(cluster gp_pop) bwselect(msetwo) covs(female_seat)
di "b" round(e(tau_bc),.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb),.01) " "  round(e(ci_r_rb),.01) "]   "

}
*



foreach out in educ_years age prob_obc_reweight {

qui rdrobust `out' gp_pop if winner==1 & inrange(gp_pop,`b'-1000,`b'+999) & (seatt==7 | seatt==8), c(`b') all fuzzy(cand) vce(cluster gp_pop) bwselect(msetwo)
di "b" round(e(tau_bc),.001) "  p-val: " round(e(pv_rb),.001) "  ci:["round(e(ci_l_rb),.01) " "  round(e(ci_r_rb),.01) "]   "

}
*

}
*

}
*




* Table A15, Panels 3-7
{
use "$data/vill_lgd_census11_01.dta", clear
merge 1:1 lgd_id using "$data/temp_3.dta", gen(merge0) 

drop if gp==.
merge 1:m gp using "$data/elecdata_clean.dta", gen(merge00)
keep if merge00==3

gen female_seat = (seatt==2 | seatt==4 | seatt==6 | seatt==7)
keep if winner==1 & inrange(gp_pop,3001,10000)

duplicates drop gp ward_id, force


* Expand dataset in order to implement rdmc
{
expand 2
bysort gp ward_id: gen copy = _n

gen 	c = 4001 if gp_pop>=3001 & gp_pop<5001 & copy==1
replace c = 6001 if gp_pop>=5001 & gp_pop<7001 & copy==1
replace c = 8001 if gp_pop>=7001 & gp_pop<9001 & copy==1

replace c = 5001 if gp_pop>=4001 & gp_pop<6001 & copy==2
replace c = 7001 if gp_pop>=6001 & gp_pop<8001 & copy==2
replace c = 9001 if gp_pop>=8001 & gp_pop<10001 & copy==2
}
*


gen bwselect = "msetwo" in 1
forv n = 2/6 {
replace bwselect = "msetwo" in `n'
}

gen vce = "cluster gp_pop" in 1
forv n = 2/6 {
replace vce = "cluster gp_pop" in `n'
}


* Panel 3
{
foreach out in candidates {

qui rdmc `out' gp_pop, c(c) bwselect(bwselect) vce(vce)

lincom c2

lincom c2 - (c1 + c3)/2 - (c5 - (c4 + c6)/2)

lincom c2 - (c1*9/11 + c3*13/11)/2 - (c5*17/11 - (c4*15/11 + c6*19/11)/2)
}
*
}
*



* Panels 4, 5, 7
foreach out in educ_years_winner age_winner prob_obc_reweight {

qui rdmc `out' gp_pop if seatt==7 | seatt==8, c(c) bwselect(bwselect) vce(vce)

lincom c2

lincom c2 - (c1 + c3)/2 - (c5 - (c4 + c6)/2)

lincom c2 - (c1*9/11 + c3*13/11)/2 - (c5*17/11 - (c4*15/11 + c6*19/11)/2)

}
*



* Panel 6
{
{
gen covs = "female_seat" in 1
forv n = 2/6 {
replace covs = "female_seat" in `n'
}



foreach out in female_winner {

qui rdmc `out' gp_pop if seatt==7 | seatt==8, c(c) bwselect(bwselect) covs(covs) vce(vce)

lincom c2

lincom c2 - (c1 + c3)/2 - (c5 - (c4 + c6)/2)

lincom c2 - (c1*9/11 + c3*13/11)/2 - (c5*17/11 - (c4*15/11 + c6*19/11)/2)

}
*

}
*


}
*

}
*
